package com.hanlp.top_interview_150.数组;

/**
 * 122. 买卖股票的最佳时机 II
 */
public class A08 {

    public static void main(String[] args) {
        // 7
        int i = maxProfit(new int[]{7, 1, 5, 3, 6, 4});
        System.out.println(i);

        i = maxProfit(new int[]{1,2,3,4,5});
        System.out.println(i);
    }


    public static int maxProfit(int[] prices) {
        int buy = prices[0];
        //
        int lastDayIncomePrice = prices[0];
        int sum = 0;
        for (int i = 1; i < prices.length; i++) {
            if(prices[i] >= lastDayIncomePrice){
                lastDayIncomePrice = prices[i];
            }else { // 卖出时机
                sum = sum + lastDayIncomePrice - buy;
                buy = prices[i];
                lastDayIncomePrice = prices[i];
            }
        }
        sum = sum + lastDayIncomePrice -buy;
        return sum;
    }

    public int maxProfitPlus(int[] prices) {
        int Profit=0;
        for(int i=1;i<prices.length;i++){
            if(prices[i]>prices[i-1]){
                Profit+=prices[i]-prices[i-1];
            }
        }
        return Profit;
    }

}
